package com.what21.netfwd;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author С�ܶ� (http://www.what21.com)
 */
public class ForwardService {

	private ForwardMapping mapping;

	/**
	 * @param config
	 * @param mapping
	 */
	public ForwardService(String config, String mapping) {
		config(config);
		this.mapping = new ForwardMapping(mapping);
	}

	/**
	 * @param config
	 * @param mapping
	 */
	public ForwardService(File config, File mapping) {
		config(config);
		this.mapping = new ForwardMapping(mapping);
	}

	/**
	 * @param config
	 */
	private void config(String config) {
		config(ForwardService.class.getResourceAsStream(config));
	}

	/**
	 * @param config
	 */
	private void config(File config) {
		try {
			config(new FileInputStream(config));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	private void config(InputStream input) {
		Properties props = new Properties();
		try {
			props.load(input);
		} catch (IOException e) {
			e.printStackTrace();
		}
		int buffer = 10240;
		try {
			buffer = Integer.parseInt(props.getProperty("buffer", "10240"));
		} catch (Exception localException) {
		}
		EnvUtil.BUFFER_SIZE = buffer;
	}

	public void startup() {
		new NetServer(this.mapping).start();
	}

}
